package com.ayf.payment.game.api.service.impl;

import com.ayf.payment.game.api.entity.TemplateAdditional;
import com.ayf.payment.game.api.exception.ParameterException;
import com.ayf.payment.game.api.mapper.TemplateAdditionalMapper;
import com.ayf.payment.game.api.service.TemplateAdditionalService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TemplateAdditionalServiceImpl implements TemplateAdditionalService {

    @Resource
    private TemplateAdditionalMapper templateAdditionalMapper;

    @Override
    public boolean insertAdditionals(List<TemplateAdditional> additionalList) {
        if (additionalList == null) {
            throw new ParameterException("附加赠送参数不能为空");
        }
        return templateAdditionalMapper.insertAdditionals(additionalList) > 0;
    }

    @Override
    public List<TemplateAdditional> selectAllByTemplateId(String templateId) {
        TemplateAdditional additional = checkParameter(templateId);
        return templateAdditionalMapper.selectByTemplateId(templateId);
    }

    private TemplateAdditional checkParameter(String templateId) {
        if (StringUtils.isBlank(templateId)) {
            throw new ParameterException("模板id不能为空");
        }
        TemplateAdditional additional = new TemplateAdditional();
        additional.setTemplateId(templateId);
        return additional;
    }

    @Override
    public boolean delAdditionalByTemId(String templateId) {
        TemplateAdditional additional = checkParameter(templateId);
        return templateAdditionalMapper.delete(additional) > 0;
    }
}


